package com.rzico.sbl.ui.mine

import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.core.view.setPadding
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityCommonListBinding
import com.rzico.sbl.model.WalletBillData
import com.rzico.sbl.other.LinearNormalDecoration
import com.rzico.sbl.other.getTodayDate
import com.rzico.sbl.viewmodel.WalletViewModel
import com.xinnuo.common.extend.*
import com.xinnuo.common.helper.*
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.base.count
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.common_ui.utils.showTimeDialog
import com.xinnuo.slimadapter.SlimAdapter

class WalletBillActivity : BaseActivity(R.layout.activity_common_list) {

    private val mBinding by lazy { ActivityCommonListBinding.bind(getRootView()) }
    private var mBillDate = getTodayDate()

    override fun getViewModel() = get<WalletViewModel>()

    override fun initData() {
        initTitle(title = "账单流水")

        initLayout()

        mBinding
            .listLayout
            .swipeRefresh
            .isRefreshing = true

        getData(pageNum)
    }

    private fun initLayout() {
        mBinding.listLayout.apply {
            emptyLayout.also {
                it.emptyHint.text = "暂无相关账单信息！"
            }

            recycleList.also { view ->
                view.loadLinear(
                    refresh = swipeRefresh,
                    onRefresh = { getData(0) },
                    onLoad = {
                        if (!isLoadingMore) {
                            isLoadingMore = true
                            getData(pageNum)
                        }
                    }
                )

                view.addItemDecoration(
                    object : LinearNormalDecoration() {
                        override fun getHeaderName(pos: Int): String =
                            "${(mList[pos] as WalletBillData).letter}月"
                    }.apply {
                        setHeaderContentColor(getColorEx(R.color.background))
                        setTextColor(getColorEx(R.color.black))

                        setHeaderHeight(dp2px(40f))
                        setFooterHeight(dp2px(13f))
                        setTextSize(sp2px(15f))
                    }
                )

                mAdapter = SlimAdapter.creator()
                    .register<WalletBillData>(R.layout.item_wallet_bill) { jector, bean, _ ->
                        jector.apply {
                            text(R.id.item_bill_name, bean.memo)
                            text(R.id.item_bill_time, bean.createDate.getTimeString())
                            text(R.id.item_bill_remain, "余额: ${bean.balance.formatDecimal()}")

                            with<TextView>(R.id.item_bill_num) {
                                val amount = bean.amount.doubleValue
                                it.setTextColor(
                                    getColorEx(
                                        if (amount < 0) R.color.red
                                        else R.color.black
                                    )
                                )

                                it.text = "${if (amount < 0) "" else "+"}${amount.formatDecimal()}"
                            }

                            with<ImageView>(R.id.item_bill_img) { ivImg ->
                                ivImg.setImageResource(
                                    when (bean.type) {
                                        "product" -> R.mipmap.icon_mine12
                                        "payment" -> R.mipmap.icon_report10
                                        else -> R.mipmap.icon_mine13
                                    }
                                )
                            }
                        }
                    }
                    .attachTo(view)
            }
        }

        ivRight?.apply {
            setPadding(dp2px(14f))
            setImageResource(R.mipmap.icon_statistic06)
            visible()

            oneClick {
                showTimeDialog(
                    title = "请选择日期",
                    count = 3,
                    isCurrentDate = true,
                    isAfterLimited = true
                ) { _, _, _, _, _, hint ->
                    mBillDate = hint
                    updateList()
                }
            }
        }
    }

    /**
     * 账单流水列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .billList(
                page = index,
                billDate = mBillDate,
                onSuccess = {
                    mList.apply {
                        if (index == 0) {
                            clear()
                            pageNum = 0
                        }
                        addItems(it)
                        if (it.count() > 0) pageNum++
                    }

                    mList.onEach {
                        it as WalletBillData
                        it.letter = TimeHelper
                            .getInstance()
                            .getMonthOfDate(
                                it.createDate.getTimeString(),
                                "yyyy-MM-dd HH:mm:ss"
                            )
                    }

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    isLoadingMore = false

                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

    /**
     * 刷新列表数据
     */
    private fun updateList() {
        mBinding
            .listLayout
            .apply {
                swipeRefresh.isRefreshing = true

                emptyLayout
                    .emptyView
                    .gone()
            }

        if (mList.isNotEmpty()) {
            mList.clear()
            mAdapter.notifyDataSetChanged()
        }

        getData(0)
    }

}